//
//  WBUserModel.m
//  TransWB
//
//  Created by 孝飞王 on 2017/1/10.
//  Copyright © 2017年 孝飞王. All rights reserved.
//

#import "WBUserModel.h"

@implementation WBUserModel

- (NSString *)description
{
    return  [self yy_modelDescription];
}

- (void)setExpires_in:(double)expires_in{
    
    _expires_in = expires_in;
    //加上就崩溃这是为什么呢
    //self.expiresDate = [NSDate dateWithTimeIntervalSinceNow:expires_in];
}

//钥匙串储存密码  得用框架储存 叫SSKeyChain
-(void)saveAccount{
   // self.exprite_date = [NSDate dateWithTimeIntervalSinceNow:self.expires_in];

    NSLog(@"%@",self );
        NSLog(@"%@",[self yy_modelToJSONObject]);
    NSDictionary *dictbase = (NSDictionary*)([self yy_modelToJSONObject]) ;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dictbase];
   // [dict removeObjectForKey:@"expires_in"];
    //[dict setValue:[NSDate dateWithTimeIntervalSinceNow:self.expires_in] forKey:@"exprite_date"];
    
    //字典序列化
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    
    //写入磁盘
    NSString *pathName  = [User_Account_Key cz_appendDocumentDir];
    [data writeToFile:pathName atomically:YES];
    NSLog(@"path  - - -    %@",pathName);
    
}



//重写父类方法
- (instancetype)init
{
    self = [super init];
    if (self) {
        
       //1 从磁盘加载数据
        NSString *pathString = [User_Account_Key cz_appendDocumentDir];
        NSData *data  = [NSData dataWithContentsOfFile:pathString];
        if (!data) return self;
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [self yy_modelSetWithDictionary:dict];

        //2设置字典
        
        //设置日期
       // self.exprite_date = [NSDate dateWithTimeIntervalSinceNow:self.expires_in];
        
//        NSLog(@"%@",self);
        
        //3 判断过期日期  现在的日期比之前的多
        //    1               >=           2
        if ([[NSDate dateWithTimeIntervalSinceNow:self.expires_in] compare:[NSDate date]]  != NSOrderedDescending) {
            NSLog(@"过期");
            
            //清空token
            self.access_token = nil;
            self.uid = nil;
            
            //删除文件
            [[NSFileManager defaultManager] removeItemAtPath:pathString error:nil];
            
            
        }
        
    }
    return self;
}




@end
